package com.tianhui.consignortwofei.mvp.ui.fragment;

import android.text.Editable;
import android.text.TextWatcher;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseDataListFragment;
import com.xuexiang.xui.widget.edittext.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/8/27
 * @description
 **/
public abstract class SearchFragment<V extends CommonView, P extends CommonPresenter<V>> extends BaseDataListFragment<V, P> {

    @BindView(R.id.layout_order_search_searchKeyEditText)
    ClearEditText mSearchKeyEditText;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_source_list;
    }
    public void setSearchKeyHint(String hint) {
        if (mSearchKeyEditText != null) {
            mSearchKeyEditText.setHint(hint);
        }
        mSearchKeyEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                String searchKey = s.toString();

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                String searchKey = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {
                String searchKey = s.toString();
                try {
                    searchKey=searchKey.replaceAll(" +","");
                }catch (Exception e){
                    LogUtil.print(e.toString());
                }
                changeSearchKey(searchKey);
            }
        });
    }

    protected abstract void requestSearch(String searchKey);

    protected abstract void changeSearchKey(String searchKey);

    @OnClick(R.id.layout_order_search_searchTextView)
    public void search() {
        String searchKey = mSearchKeyEditText.getText().toString().trim();
        try {
            searchKey=searchKey.replaceAll(" +","");
        }catch (Exception e){
            LogUtil.print(e.toString());
        }

        requestSearch(searchKey);
    }

}
